/**
 * Created by Administrator on 2017/5/25 0025.
 */


$(function(){
    var flag=false;
    var flag1=false;
    var flag2=false;

    $("#btn1").click(function(){
        $("#uname").blur();
        $("#upwd").blur();
        $("#upwd1").blur();

        if(flag && flag1 && flag2){
            $.ajax({
                url:"http://127.0.0.1/myphp/reg.php",
                type:"post",
                data:$("#form").serialize(),
                success:function(result){
                    var obj=JSON.parse(result);
                    if(obj.status==1){
                        $(".txt").css("display","block").text(obj.msg+"即将返回登录界面").css("color","green");
                        var i=5;
                        var txt= $(".txt");
                        var b=$("<b></b>");
                        var timer=setInterval(function(){

                            b.html(i+"秒");
                            i--;
                            b.appendTo(txt);

                            if(i<=0){
                                clearInterval(timer);
                                window.location.href="login.html";
                            }
                        },1000)

                    }else{
                        $(".txt").css("display","block").text(obj.msg);
                    }
                }
            })
        }

    });

    //手机号验证
    $("#uname").blur(function(){
        var uname=$("#uname").val();
        var reg=/^1[3|4|5|8][0-9]\d{4,8}$/;
        if(!reg.test(uname)){
            $(".txt").css("display","block").text("手机号不合法");
            $("#uname").focus();

            return false;
        }else{
            $(".txt").css("display","none");

            flag=true;
            return true;
        }
    })

    //密码验证
    $("#upwd").blur(function(){
        var upwd=$("#upwd").val();
        var reg=/^[A-Za-z0-9]{6,12}$/;
        if(!reg.test(upwd)){
            $(".txt").css("display","block").text("密码不合法");
            $("#upwd").focus();

            return false;
        }else{
            $(".txt").css("display","none");
            flag1=true;

            return true;
        }
    })
    
    $("#upwd1").blur(function(){
        var upwd1=$("#upwd1").val();
        var upwd=$("#upwd").val();
        if(upwd1!=upwd){

            $(".txt").css("display","block").text("两次密码不一致");
            $("#upwd1").focus();

            return false;
        }else{
            $(".txt").css("display","none");
            flag2=true;

            return true;
        }
    })



    //.login-tab
    $(".login-tab a").click(function(){
        $(this).addClass("active").closest("li").siblings().find("a").removeClass("active");
    })
    
    
    //登录
    $("#btn-login").click(function(){
        $.ajax({
            url:"http://127.0.0.1/myphp/login.php",
            type:"post",
            data:$("#fm").serialize(),
            success:function(result){
                var obj=JSON.parse(result);
                if(obj.status==1){
                    window.location.href="index.html";
                }else{
                    $("#tips").css("display","block").text(obj.msg);
                }
            }
        })
    })
    
})